<template>
  <div class="app-container">
    <el-row :gutter="20">
      <splitpanes :horizontal="this.$store.getters.device === 'mobile'" class="default-theme">
        <!--仓库数据-->
        <pane size="16">
          <el-col>
            <div class="head-container">
              <el-input v-model="deptName" placeholder="请输入仓库名称" clearable size="small" prefix-icon="el-icon-search" style="margin-bottom: 20px" />
            </div>
            <div class="head-container">
              <el-tree :data="deptOptions" :props="defaultProps" :expand-on-click-node="false" :filter-node-method="filterNode" ref="tree" node-key="id" default-expand-all highlight-current @node-click="handleNodeClick" />
            </div>
          </el-col>
        </pane>
        <!--仓库数据-->
        <pane size="84">
          <el-col>
              <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
                <el-form-item label="名称" prop="name">
                  <el-input
                    v-model="queryParams.name"
                    placeholder="请输入名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                  <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
              </el-form>

              <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                  <el-button
                    type="primary"
                    plain
                    icon="el-icon-plus"
                    size="mini"
                    @click="handleAdd"
                    v-hasPermi="['system:inventorys:add']"
                  >新增</el-button>
                </el-col>
                <el-col :span="1.5">
                  <el-button
                    type="success"
                    plain
                    icon="el-icon-edit"
                    size="mini"
                    :disabled="single"
                    @click="handleUpdate"
                    v-hasPermi="['system:inventorys:edit']"
                  >修改</el-button>
                </el-col>
                <el-col :span="1.5">
                  <el-button
                    type="danger"
                    plain
                    icon="el-icon-delete"
                    size="mini"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['system:inventorys:remove']"
                  >删除</el-button>
                </el-col>
                <el-col :span="1.5">
                  <el-button
                    type="warning"
                    plain
                    icon="el-icon-download"
                    size="mini"
                    @click="handleExport"
                    v-hasPermi="['system:inventorys:export']"
                  >导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
              </el-row>

              <el-table v-loading="loading" :data="inventorysList" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="id" align="center" prop="id" v-if="false"/>
                <el-table-column label="名称" align="center" prop="name" v-if="false"/>
                <el-table-column label="仓库" align="center" prop="stockName" />

                <el-table-column label="商品" align="center" prop="goodsName" />
                <el-table-column label="库存数量" align="center" prop="goodsCount" />
                <el-table-column label="计量单位" align="center" prop="model" />

                <el-table-column label="包数" align="center" prop="packageNum" />
                <el-table-column label="立方" align="center" prop="cube" />
                <el-table-column label="创建时间" align="center" prop="createTime" />
                <el-table-column label="创建人" align="center" prop="createUserId" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-edit"
                      @click="handleUpdate(scope.row)"
                      v-hasPermi="['system:inventorys:edit']"
                    >修改</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleDelete(scope.row)"
                      v-hasPermi="['system:inventorys:remove']"
                    >删除</el-button>
                    <el-button
                      size="mini"
                      type="text"
                      icon="el-icon-delete"
                      @click="handleRefresh(scope.row)"
                      v-hasPermi="['system:inventorys:refresh']"
                    >刷新库存</el-button>
                  </template>
                </el-table-column>
              </el-table>

              <pagination
                v-show="total>0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
              />

              <!-- 添加或修改库存对话框 -->
              <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库" prop="stockId">
          <el-select v-model="form.stockId" placeholder="请选择仓库" clearable filterable>
            <el-option
              v-for="driver in this.stocksInfoList"
              :key="driver.id"
              :label="driver.name"
              :value="driver.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="货物" prop="goodsId">
          <el-select v-model="form.goodsId" placeholder="请选择货物" clearable filterable>
            <el-option
              v-for="goods in this.goodsList"
              :key="goods.id"
              :label="goods.name"
              :value="goods.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label=" 数量" prop="goodsCount">
          <el-input v-model="form.goodsCount" placeholder="请输入 数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
          </el-col>
        </pane>
      </splitpanes>
    </el-row>
  </div>
</template>

<script>
import { listInventorys, getInventorys, delInventorys, addInventorys, updateInventorys,refreshInventory } from "@/api/system/inventorys"
import {getGoodsInfoList } from "@/api/system/goodss"
import {getStocksInfoList,stockTreeSelect } from "@/api/system/stocks"
import Treeselect from "@riophae/vue-treeselect"
import "@riophae/vue-treeselect/dist/vue-treeselect.css"
import { Splitpanes, Pane } from "splitpanes"
import "splitpanes/dist/splitpanes.css"

export default {
  name: "Inventorys",
  components: { Treeselect, Splitpanes, Pane },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库存表格数据
      inventorysList: [],
      goodsList:[],
      stocksInfoList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        stockId: null,
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 部门树选项
      deptOptions: undefined,
      // 部门名称
      deptName: undefined,
      // 表单校验
      rules: {
      }
    }
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getList()
    this.getGoodsList()
    this.getStocksList()
    this.getDeptTree()
  },
  methods: {

    /** 查询部门下拉树结构 */
    getDeptTree() {
      stockTreeSelect().then(response => {
        this.deptOptions = response.data
        this.enabledDeptOptions = this.filterDisabledDept(JSON.parse(JSON.stringify(response.data)))
      })
    },
    // 过滤禁用的部门
    filterDisabledDept(deptList) {
      return deptList.filter(dept => {
        if (dept.disabled) {
          return false
        }
        if (dept.children && dept.children.length) {
          dept.children = this.filterDisabledDept(dept.children)
        }
        return true
      })
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.stockId = data.id
      this.handleQuery()
    },

    /** 查询库存列表 */
    getList() {
      this.loading = true
      listInventorys(this.queryParams).then(response => {
        this.inventorysList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tenantId: null,
        createTime: null,
        updateTime: null,
        createUserId: null,
        createOrgId: null,
        createUserName: null,
        updateUserId: null,
        updateUserName: null,
        isDelete: null,
        orgId: null,
        name: null,
        stockId: null,
        goodsId: null,
        goodsCount: null,
        createOrgName: null,
        packageNum: null,
        cube: null,
        remarks: null
      }
      this.resetForm("form")
    },

    /** 获取商品列表 */
    getGoodsList() {
      // 这里需要替换为实际的API调用
      getGoodsInfoList(this.paramName).then(response => {
        this.goodsList = response.rows
      })
    },

    /** 获取仓库列表 */
    getStocksList() {
      // 这里需要替换为实际的API调用
      getStocksInfoList(this.paramName).then(response => {
        this.stocksInfoList = response.rows
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.getGoodsList()
      this.getStocksList()
      this.open = true
      this.title = "添加库存"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.getGoodsList()
      this.getStocksList()
      const id = row.id || this.ids
      getInventorys(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改库存"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInventorys(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addInventorys(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除库存编号为"' + ids + '"的数据项？').then(function() {
        return delInventorys(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    handleRefresh(row) {
      const ids = row.id
      this.$modal.confirm('是否刷新库存编号为"' + ids + '"的数据项？').then(function() {
        return delInventorys(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/inventorys/export', {
        ...this.queryParams
      }, `inventorys_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
